package com.taotao.manage.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.Log4jLoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.manage.pojo.ContentCategory;
import com.taotao.manage.service.ContentCategoryService;

/**
 * 内容分类controller
 * @author Beck
 *
 */
@RequestMapping("content/category")
@Controller
public class ContentCategoryController {
    @Autowired
    private ContentCategoryService contentCategoryService;
    
    // 日志
    private static final Logger LOGGER = LoggerFactory.getLogger(ContentCategoryController.class);
    
    /**
     * 删除节点
     * 异步
     * 参数：parentId,父节点的id；id：要删除的节点id
     * 返回值: 无
     */
    @RequestMapping(method=RequestMethod.DELETE)
    public ResponseEntity<Void> deleteContentCategory(@RequestParam("parentId") Long parentId,
            @RequestParam("id") Long id){
        try {
            boolean flag = contentCategoryService.deleteContentCategory(parentId, id);
            if (flag){
                // 删除成功
                if (LOGGER.isDebugEnabled()){
                    LOGGER.debug("删除内容节点成功，id为：{}", id);
                }
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    
    
    /**
     * 修改内容节点
     * 异步
     * 参数：id，节点id；name，更新的名字
     * 返回结果: 无
     */
    @RequestMapping(method=RequestMethod.PUT)
    public ResponseEntity<Void> updateContentCategory(@RequestParam("id") Long id,
            @RequestParam("name") String name){
        try {
            ContentCategory record = new ContentCategory();
            record.setId(id);
            record.setName(name);
            boolean flag = contentCategoryService.update(record );
            if (flag){
                // 新增成功
                if (LOGGER.isDebugEnabled()){
                    LOGGER.debug("更新内容节点成功id：{}，name : {}", id,name);
                }
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    
    /**
     * 新增内容分类节点
     * 异步
     * 参数：parentId,父节点的id；name，新节点的名称
     * 返回值: 新增的ContentCategory
     */
    @RequestMapping(method=RequestMethod.POST)
    public ResponseEntity<ContentCategory> addContentCategory(@RequestParam("parentId") Long parentId,
            @RequestParam("name") String name){
        try {
            ContentCategory contentCategory= contentCategoryService.saveAndReturn(parentId, name);
            return ResponseEntity.status(HttpStatus.CREATED).body(contentCategory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
    
    /**
     * 根据分类的id获取所有的子分类
     * 异步
     * 参数 ： 内容父类的id,无参数就设置为0
     * 返回值: List<ContentCategory>
     */
    @RequestMapping(method=RequestMethod.GET)
    public ResponseEntity<List<ContentCategory>> queryContentCategoryByParentId(@RequestParam(value="id", defaultValue="0") 
            Long parentId){
        try {
            ContentCategory record = new ContentCategory();
            record.setParentId(parentId);
            record.setStatus(1);
            List<ContentCategory> list = contentCategoryService.queryListByWhere(record );
            // 200
            return ResponseEntity.status(HttpStatus.OK).body(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}
